package com.lczc.soa.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lczc.soa.domain.ApplyAgain;
import com.lczc.soa.domain.SearchApplyAgainResponse;
import com.lczc.soa.domain.ApplyAgain;
import com.lczc.soa.domain.ApplyAgainExample;
import com.lczc.soa.mapper.ApplyAgainMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ApplyAgainServiceImpl implements ApplyAgainService {

    @Autowired
    private ApplyAgainMapper applyAgainMapper;

    public int saveApplyAgain(ApplyAgain applyAgain) {
        if(applyAgain.getAgainId()!=null){
            if (applyAgain.getState().equals("1")){
                applyAgain.setState("2");
            }else if (applyAgain.getState().equals("2")){
                applyAgain.setState("3");
            }
            applyAgain.setUpdateDate(new Date());
            return applyAgainMapper.updateByPrimaryKeySelective(applyAgain);

        }else {
            applyAgain.setState("1");
            return applyAgainMapper.insert(applyAgain);
        }
    }


    public ApplyAgain findApplyAgainByPrimaryKey(String id) {
        return applyAgainMapper.selectByPrimaryKey(id);
    }

    public int deleteApplyAgain(String id) {
        return applyAgainMapper.deleteByPrimaryKey(id);
    }

    @Override
    public SearchApplyAgainResponse findAllApplyAgain(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ApplyAgain> applyAgains = applyAgainMapper.selectByExample(new ApplyAgainExample());
        return new SearchApplyAgainResponse(((Page)applyAgains).getTotal(),applyAgains);
    }
    @Override
    public SearchApplyAgainResponse findApplyAgainLikeName(String keywords, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ApplyAgain> ApplyAgains = applyAgainMapper.selectByLikeName(keywords);
        return new SearchApplyAgainResponse(((Page)ApplyAgains).getTotal(),ApplyAgains);
    }

    public List<ApplyAgain> listApplyAgain() {
        return applyAgainMapper.selectAll();
    }

    public ApplyAgain findApplyAgainByName(String userName) {
        return applyAgainMapper.selectByName(userName);
    }
}
